package com.comac.print.component.app.command.cmdExe;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.alibaba.cola.dto.Response;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.comac.mom.simpleintegration.mq.MQProducerComponent;
import com.comac.print.component.app.common.PrintCommon;
import com.comac.print.component.app.common.PrintContext;
import com.comac.print.component.app.dto.MasterPlateDTO;
import com.comac.print.component.app.gatewayimpl.database.dataobject.MasterPlateDO;
import com.comac.print.component.app.gatewayimpl.database.mapper.MasterPlateMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

@Slf4j
@Component
public class PrintCmdExe {

    @Autowired
    private MasterPlateMapper masterPlateMapper;
    @Autowired
    private FoFollowCmdExe foFollowCmdExe;
    @Autowired
    MQProducerComponent mqProducerComponent;

    public Response print(PrintContext printContext) throws Exception {
        MasterPlateDO masterPlateDO = getMasterPlate(printContext);
        PrintCommon printCommon = buildPrintCommon(printContext,masterPlateDO);
        return foFollowCmdExe.executeSync(printCommon);
    }

    /**
     * @user CQ
     * @date 2025-5-12 14:54:42
     * @param printContext
     * @return 获取模板信息
     * @throws Exception
     */
    private MasterPlateDO getMasterPlate(PrintContext printContext) throws Exception {
        //1.调用系统表校验签名资质
        QueryWrapper<MasterPlateDO> masterPlateWrapper = new QueryWrapper<>();
        String rePrintDate = printContext.getRePrintDate();
        LocalDateTime now;
        if (StringUtils.isEmpty(rePrintDate)) {
            now = LocalDateTimeUtil.now();
        } else {
            now = LocalDateTimeUtil.parse(rePrintDate, "yyyy-MM-dd HH:mm:ss");
        }
        Date from = Date.from(now.atZone(ZoneId.systemDefault()).toInstant());
        masterPlateWrapper.lambda().eq(MasterPlateDO::getBussinessId, printContext.getBussinessId())
                .eq(MasterPlateDO::getSystemId, printContext.getSystemId())
                .eq(MasterPlateDO::getDeleteFlag, 0)
                .le(MasterPlateDO::getValidStart, from)
                .gt(MasterPlateDO::getValidEnd, from);
        List<MasterPlateDO> masterPlateDOList = masterPlateMapper.selectList(masterPlateWrapper);
        if (CollectionUtil.isEmpty(masterPlateDOList)) {
            throw new Exception("未查询到当前场景的模板!");
        }
        if (masterPlateDOList.size() > 1) {
            throw new Exception("符合当前场景的模板有多个!");
        }
        return masterPlateDOList.get(0);
    }

    private PrintCommon buildPrintCommon(PrintContext printContext, MasterPlateDO masterPlateDO){
        PrintCommon printCommon = new PrintCommon();
        MasterPlateDTO masterPlateDTO = new MasterPlateDTO();
        BeanUtils.copyProperties(masterPlateDO, masterPlateDTO);
        /*String s = JSONObject.toJSONString();
        JSONObject parseObject = JSON.parseObject(s);*/
        //打印模板对象
        printCommon.setMasterPlateDTO(masterPlateDTO);
        //打印数据
        printCommon.setData(printContext.getData());
        printCommon.setSystemId(printContext.getSystemId());
        printCommon.setBussinessId(printContext.getBussinessId());
        return printCommon;
    }

}
